package com.lishe.lishe.lishe.module.excitation.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lishe.lishe.Imageloader.ImageLoaderUtils;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.module.excitation.bean.HeadInfo;

import java.util.List;

/**
 * Created by shino on 2017/11/23.
 */

public class ViewpagerAdapter extends PagerAdapter {
    private List<HeadInfo> users;
    private Context context;

    public ViewpagerAdapter(Context context,List<HeadInfo> users) {
        this.context = context;
        this.users = users;
    }

    @Override
    public int getCount() {
        return users == null ? 0 : users.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = getView(users.get(position));
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    private View getView( HeadInfo user){
        View view = LayoutInflater.from(context).inflate(R.layout.item_user_viewpager, null);
        ImageView headImg = view.findViewById(R.id.headImg);
        TextView name = view.findViewById(R.id.name);
        TextView job = view.findViewById(R.id.job);
        LinearLayout buttomLayout = view.findViewById(R.id.buttomLayout);
        TextView time = view.findViewById(R.id.time);
        ImageLoaderUtils.displayImage(user.getAvatarPath(), headImg, R.mipmap.head);
        name.setText(user.getEmpName());
        String str = "";
        if (!TextUtils.isEmpty(user.getDeptName())) {
            str = user.getDeptName();
        }
        if (!TextUtils.isEmpty(user.getDeptName()) && !TextUtils.isEmpty(user.getPosName())) {
            str = str + "·" + user.getPosName();
        } else {
            if (!TextUtils.isEmpty(user.getPosName())) {
                str = str + user.getPosName();
            }
        }
        job.setText(str);
        if(TextUtils.isEmpty( user.getEntryDate())){
            buttomLayout.setVisibility(View.GONE);
        }else{
            buttomLayout.setVisibility(View.VISIBLE);
            time.setText("-" + user.getEntryDate() + "-");
        }
        return view;
    }

}
